import {Injectable} from '@nestjs/common';
// ORM
import {InjectRepository} from '@nestjs/typeorm';
import {Brackets, Repository, UpdateResult, DeleteResult} from 'typeorm';
import {EventCategoryEntity} from '../entity/event.category.entity';
import {EventTypeEntity} from '../entity/event.type.entity';
import {EventCategoryDto} from '../dto/event.category.dto';
import {EventTypeDto} from '../dto/event.type.dto';
import {ApplyDeviceEntity} from "../entity/apply.device.entity";
import {EventCategoryTypeStatus} from "../common/constants";

@Injectable()
export class EventService {
    constructor(
        @InjectRepository(EventCategoryEntity)
        private readonly eventCategoryRepository: Repository<EventCategoryEntity>,
        @InjectRepository(EventTypeEntity)
        private readonly eventTypeRepository: Repository<EventTypeEntity>,
    ) {
    }

    async createEventCategory(categoryDto: EventCategoryDto): Promise<EventCategoryEntity> {
        return await this.eventCategoryRepository.save(categoryDto);
    }

    async createEventType(eventTypeDto: EventTypeDto): Promise<EventTypeEntity> {
        return await this.eventTypeRepository.save(eventTypeDto);
    }

    async editEventCategory({id, name}): Promise<UpdateResult> {
        return await this.eventCategoryRepository.update({id}, {name});
    }

    async editEventType({id, name, categoryId, code}): Promise<UpdateResult> {
        return await this.eventTypeRepository.update({id}, {name, categoryId, code});
    }

    async editEventCategoryStatus({id, status}): Promise<UpdateResult> {
        return await this.eventCategoryRepository.update({id}, {status});
    }

    async editEventTypeStatus({id, status}): Promise<UpdateResult> {
        return await this.eventTypeRepository.update({id}, {status});
    }

    async delEventCategory(id) :Promise<DeleteResult> {
        try {
            await this.eventTypeRepository.delete({categoryId: id})
        } catch(e){
            console.log(e)
        }
        return await this.eventCategoryRepository.delete({id})
    }

    async delEventType(id) :Promise<DeleteResult> {
        return await this.eventTypeRepository.delete({id})
    }
    async getAllEventCategory(): Promise<EventCategoryEntity[]> {
        const qb = this.eventCategoryRepository.createQueryBuilder('ec');
        qb.where({status: EventCategoryTypeStatus.NORMAL})
        return qb.getMany()
    }

    async getAllEventTypeByCategoryId(categoryId: string): Promise<EventTypeEntity[]> {
        const qb = this.eventTypeRepository.createQueryBuilder('et');
        qb.where({categoryId, status: EventCategoryTypeStatus.NORMAL})
        return qb.getMany()
    }

    async getEventCategoryByName(name: string): Promise<EventCategoryEntity> {
        const qb = this.eventCategoryRepository.createQueryBuilder('ec');
        qb.where({name, status: EventCategoryTypeStatus.NORMAL})
        return qb.getOne()
    }

    async getEventTypeByNameOrCode(name: string, code: string): Promise<EventCategoryEntity> {
        const qb = this.eventTypeRepository.createQueryBuilder('et');
        qb.where({status: EventCategoryTypeStatus.NORMAL})
        qb.andWhere(new Brackets((qb) => {
            qb.where({name}).orWhere({code})
        }))
        return qb.getOne()
    }

    async getAll(): Promise<EventCategoryEntity[]> {
        const qb = this.eventCategoryRepository.createQueryBuilder('ec');
        // qb.where({status: EventCategoryTypeStatus.NORMAL})
        qb.leftJoinAndMapMany('ec.eventTypeList', EventTypeEntity, 'et', 'ec.id = et.categoryId')
        return qb.getMany()
    }
}
